#include "test_view.h"
#include "ui_test_view.h"
#include "widget_conversation.h"
#include "widget_header_buttons.h"
#include <QTimer>
#include <QPalette>

test_view::test_view(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::test_view),
    q1(false),q2(false),q3(false),q4(false)
{
    setMainWindowTitle("Test view");

    ui->setupUi(this);
    ui->gridLayout->addWidget(new widget_header_buttons(this),0,0,0,0);

    QTextEdit* const editBoxes[] = {
        ui->tb_s1q1, ui->tb_s1q2, ui->tb_s1q3, ui->tb_s1q4,
    };

    QRadioButton* const radioButtons[] = {
        ui->s1q1a, ui->s1q1b, ui->s1q1c,
        ui->s1q2a, ui->s1q2b, ui->s1q2c,
        ui->s1q3a, ui->s1q3b, ui->s1q3c,
        ui->s1q4a, ui->s1q4b, ui->s1q4c,
    };

    const int buttons_per_row = sizeof(radioButtons)/sizeof(*radioButtons)/4;

    if(g_currentSession == STUDENT_SESSION){
        ui->chatContainer->addWidget(new widget_conversation(this, "Do you have a question?","Use this to send message to proctor"));
        ui->edit->hide();
        ui->ad_section_button->hide();

        for (unsigned i = 0; i < sizeof(editBoxes)/sizeof(*editBoxes); i++) {
            editBoxes[i]->setReadOnly(true);
        }
        ui->mode->hide();
        //ui->gotostudentlist->hide();
        //ui->testinfo_button->hide();
        ui->answer_saved->hide();

        for (unsigned i = 0; i < buttons_per_row; i++) {
            connect(radioButtons[i], SIGNAL(clicked()), this, SLOT(question1saved()));
        }
        for (unsigned i = 1*buttons_per_row; i < 2*buttons_per_row; i++) {
            connect(radioButtons[i], SIGNAL(clicked()), this, SLOT(question2saved()));
        }
        for (unsigned i = 2*buttons_per_row; i < 3*buttons_per_row; i++) {
            connect(radioButtons[i], SIGNAL(clicked()), this, SLOT(question3saved()));
        }
        for (unsigned i = 3*buttons_per_row; i < 4*buttons_per_row; i++) {
            connect(radioButtons[i], SIGNAL(clicked()), this, SLOT(question4saved()));
        }
        ui->edit->hide();
        ui->mode->hide();
        ui->view_test_info->hide();
    }
    else if(g_currentSession == TEACHER_SESSION){
        connect(ui->edit,SIGNAL(clicked()),this,SLOT(edits1q1buttonclicked()));
        ui->answer_saved->hide();
        ui->ad_section_button->hide();

        connect(editBoxes[0], SIGNAL(textChanged()), this, SLOT(question1saved()));
        connect(editBoxes[1], SIGNAL(textChanged()), this, SLOT(question2saved()));
        connect(editBoxes[2], SIGNAL(textChanged()), this, SLOT(question3saved()));
        connect(editBoxes[3], SIGNAL(textChanged()), this, SLOT(question4saved()));

        for (unsigned i = 0; i < buttons_per_row; i++) {
            connect(radioButtons[i], SIGNAL(toggled(bool)), this, SLOT(question1saved()));
        }
        for (unsigned i = 1*buttons_per_row; i < 2*buttons_per_row; i++) {
            connect(radioButtons[i], SIGNAL(toggled(bool)), this, SLOT(question2saved()));
        }
        for (unsigned i = 2*buttons_per_row; i < 3*buttons_per_row; i++) {
            connect(radioButtons[i], SIGNAL(toggled(bool)), this, SLOT(question3saved()));
        }
        for (unsigned i = 3*buttons_per_row; i < 4*buttons_per_row; i++) {
            connect(radioButtons[i], SIGNAL(toggled(bool)), this, SLOT(question4saved()));
        }
        ui->submit_test->hide();
        ui->edit->hide();
        ui->mode->hide();
    }
    else if(g_currentSession == PROCTOR_SESSION){
        ui->chatContainer->addWidget(new widget_conversation(this, "What does 'arbiter' mean?","Use this to send message to student"));
        ui->edit->hide();
        ui->ad_section_button->hide();

        for (unsigned i = 0; i < sizeof(editBoxes)/sizeof(*editBoxes); i++) {
            editBoxes[i]->setReadOnly(true);
        }
        ui->submit_test->hide();
        ui->mode->hide();
        ui->answer_saved->hide();
        ui->edit->hide();
        ui->mode->hide();
    }
}

test_view::~test_view()
{
    delete ui;
}

/*
void test_view::on_testinfo_button_clicked()
{
    emit (changeScene(TEST_INFO_VIEW));
}

void test_view::on_logout_button_clicked()
{
    emit (changeScene(SIGN_IN_VIEW));
}
*/

void test_view::question1saved()
{
    q1 = true;
    QPalette plt;
    plt.setColor(QPalette::WindowText, Qt::black);
    ui->label->setPalette(plt);
    ui->answer_saved->hide();
    QTimer *timer = new QTimer(this);
    timer->setSingleShot(true);
    timer->start(250);
    connect(timer, SIGNAL(timeout()), this, SLOT(TimeOutq1saved()));
}

static QString getQuestionString(int qNumber)
{
    return QString("Question ") + QString::number(qNumber) + ":\n" + (g_currentSession == TEACHER_SESSION ? "Question saved." : "Answer saved.");
}

void test_view::TimeOutq1saved()
{
    ui->answer_saved->setText(getQuestionString(1));
    ui->answer_saved->show();
}

void test_view::question2saved()
{
    q2 = true;
    QPalette plt;
    plt.setColor(QPalette::WindowText, Qt::black);
    ui->label_2->setPalette(plt);
    ui->answer_saved->hide();
    QTimer *timer = new QTimer(this);
    timer->setSingleShot(true);
    timer->start(250);
    connect(timer, SIGNAL(timeout()), this, SLOT(TimeOutq2saved()));
}

void test_view::TimeOutq2saved()
{
    ui->answer_saved->setText(getQuestionString(2));
    ui->answer_saved->show();
}

void test_view::question3saved()
{
    q3 = true;
    QPalette plt;
    plt.setColor(QPalette::WindowText, Qt::black);
    ui->label_3->setPalette(plt);
    ui->answer_saved->hide();
    QTimer *timer = new QTimer(this);
    timer->setSingleShot(true);
    timer->start(250);
    connect(timer, SIGNAL(timeout()), this, SLOT(TimeOutq3saved()));
}

void test_view::TimeOutq3saved()
{
    ui->answer_saved->setText(getQuestionString(3));
    ui->answer_saved->show();
}

void test_view::question4saved()
{
    q4 = true;
    QPalette plt;
    plt.setColor(QPalette::WindowText, Qt::black);
    ui->label_4->setPalette(plt);
    ui->answer_saved->hide();
    QTimer *timer = new QTimer(this);
    timer->setSingleShot(true);
    timer->start(250);
    connect(timer, SIGNAL(timeout()), this, SLOT(TimeOutq4saved()));
}

void test_view::TimeOutq4saved()
{
    ui->answer_saved->setText(getQuestionString(4));
    ui->answer_saved->show();
}

void test_view::on_submit_test_clicked()
{
    QMessageBox msgBox;
    if(q1 && q2 && q3 && q4) {
        msgBox.setText("Are you sure you want to submit?");
        msgBox.setInformativeText("This cannot be undone.");
        msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        msgBox.setButtonText(QMessageBox::Ok,"Submit");
        msgBox.setDefaultButton(QMessageBox::Cancel);
    } else {
        msgBox.setText("You have not answered all questions.\nAre you sure you want to submit?");
        msgBox.setInformativeText("This cannot be undone.");
        msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setButtonText(QMessageBox::Ok,"Submit");
        msgBox.setDefaultButton(QMessageBox::Cancel);
    }


    int ret = msgBox.exec();
    if (ret == QMessageBox::Ok){
        emit(changeScene(TEST_LIST_VIEW));
    } else {
        QPalette plt;
        plt.setColor(QPalette::WindowText, Qt::red);
        if(!q1){
            ui->label->setPalette(plt);
        }
        if(!q2){
            ui->label_2->setPalette(plt);
        }
        if(!q3){
            ui->label_3->setPalette(plt);
        }
        if(!q4){
            ui->label_4->setPalette(plt);
        }
    }
}

void test_view::edits1q1buttonclicked()
{
    QTextEdit* const editBoxes[] = {
        ui->tb_s1q1, ui->tb_s1q2, ui->tb_s1q3, ui->tb_s1q4,
    };

    if (QString::compare(ui->edit->text(),"Edit Mode", Qt::CaseInsensitive))
    {
        ui->edit->setText("Edit Mode");
        for (unsigned i = 0; i < sizeof(editBoxes)/sizeof(*editBoxes); i++) {
            editBoxes[i]->setReadOnly(true);
        }
        ui->mode->setText("Currently in:\nView Mode");
    }
    else
    {
        ui->edit->setText("View Mode");
        for (unsigned i = 0; i < sizeof(editBoxes)/sizeof(*editBoxes); i++) {
            editBoxes[i]->setReadOnly(false);
        }
        ui->mode->setText("Currently in:\nEdit Mode");
    }
}

void test_view::on_view_test_info_clicked()
{
    emit(changeScene(TEST_INFO_VIEW));
}
